package com.study.demo;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.crypto.Cipher;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;

import com.sun.glass.ui.Clipboard;
import com.sun.webkit.ContextMenu.ShowContext;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.Mnemonic;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**屏幕截图例子
 * 具体步骤：
 * 		1.0设计好组件的位置
 * 		2.0当点击截图按钮时，弹出遮盖层
 * 		3.0点击遮盖层时，获取对应的坐标位置
*@description
*@author xtayhame
*date 2020年3月17日
*version
*since v1.0
*/
public class ScreenShotMain extends Application{
	Stage primaryStage;  //主界面窗口
	ImageView imageView;  //主界面存放图片
	AnchorPane shotAnchorPane;  //截图布局
	Stage shotStage;   //截图窗口
	Label label=null ;   //用于显示截取的宽和高

	HBox hBox;

	double screen_startX=0.0;   //鼠标开始点击时 X坐标
	double screen_startY=0.0;	//鼠标开始点击时 Y坐标
	double width=0.0;			//截图图形的宽
	double height=0.0;			//截图图形的高

	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage primaryStage) throws Exception {
		this.primaryStage = primaryStage;

		AnchorPane an = new AnchorPane();
		Button shotBut = new Button("截图");

		HBox hBox = new HBox();
		imageView = new ImageView();
		imageView.setPreserveRatio(true);
		imageView.setFitWidth(500);
		//imageView.setFitHeight(400);
		//imageView.setFitWidth(400);
		hBox.getChildren().add(imageView);
		hBox.setPrefSize(400, 300);
		hBox.setAlignment(Pos.CENTER);
		hBox.setStyle("-fx-background-color:#FFF555");

		an.getChildren().addAll(shotBut,hBox);
		AnchorPane.setTopAnchor(hBox, 40.0);
		AnchorPane.setLeftAnchor(hBox, 50.0);

		Scene scene = new Scene(an, 600, 500);
		primaryStage.setScene(scene);
		primaryStage.show();


		//被button添加快捷键
		KeyCombination keyCombination = KeyCombination.valueOf("Ctrl+Alt+p");
		Mnemonic mnemonic = new Mnemonic(shotBut, keyCombination);
		scene.addMnemonic(mnemonic);


		//点击截图按钮
		shotBut.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				showShotDialog();
			}
		});
	}



	//弹出截图窗口
	public void showShotDialog(){
		//主界面设置最小化, 注意，这里要先让主界面最小化，如果将这句代码放在后面，在最小化主界面的同时，遮盖层也会最小化
		primaryStage.setIconified(true);
		/*hBox = new HBox();
		shotAnchorPane.getChildren().add(hBox);*/

		shotAnchorPane = new AnchorPane();
		Scene scene = new Scene(shotAnchorPane);
		shotStage = new Stage();
		shotStage.setScene(scene);



		//设置背景颜色
		shotAnchorPane.setStyle("-fx-background-color:#ffff5522");
		//设置scene的填充颜色
		scene.setFill(Paint.valueOf("#ffff5500"));

		//设置全屏
		shotStage.setFullScreen(true);  //好像设置全屏无效
		shotStage.setIconified(false);

		//如果不设置，则会在最大化的时候会自动 显示"按ESC键可退出全屏模式"
		shotStage.setFullScreenExitHint("");
		//设置窗口透明
		shotStage.initStyle(StageStyle.TRANSPARENT);

		shotStage.show();






		//按ESC键退出， 需要给场景添加监听，然后关闭stage
		scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				if(event.getCode() == KeyCode.ESCAPE){
					shotStage.close();
					primaryStage.setIconified(false);
				}
			}
		});



		//给屏幕添加一个鼠标点击事件,这里应该是鼠标按下，而不是鼠标点击
		shotAnchorPane.setOnMousePressed(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {

				//获取当前点击的坐标
				mouseClick(event);
			}
		});

		//拖拽监测
		shotAnchorPane.setOnDragDetected(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				shotAnchorPane.startFullDrag();
			}
		});

		//鼠标划过
		shotAnchorPane.setOnMouseDragOver(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {


				double screen_endX=event.getSceneX();
				double screen_endY=event.getSceneY();


				width = screen_endX-screen_startX;
				height = screen_endY - screen_startY;

				label.setText(width + "x" + height );

				System.out.println("开始坐标：" + screen_startX+ "    "+screen_startY +"     结束坐标的位置："+event.getScreenX() + "    " + event.getScreenY() +"   宽："+width + "   高："+height);

				//有了宽高就可以设置hbox的宽和高了
				//hBox.prefHeight(height);
				//hBox.prefWidth(width);
				hBox.setPrefSize(width, height);
			}
		});


		//拖拽松手
		shotAnchorPane.setOnMouseDragReleased(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				Button saveBtn = new Button("保存");
				hBox.getChildren().add(saveBtn);
				hBox.setAlignment(Pos.BOTTOM_RIGHT);


				saveBtn.setOnAction(new EventHandler<ActionEvent>() {
					@Override
					public void handle(ActionEvent event) {
						saveImage();
					}
				});
			}
		});

	}

	//鼠标点击事件
	public void mouseClick(MouseEvent event){
		//每次清空一下
		shotAnchorPane.getChildren().clear();
		System.out.println("有清空吗："+ shotAnchorPane.getChildren().size());
		//这里要添加一个Hbox，用于显示截图的形状
		hBox = new HBox();


		hBox.setBackground(null);

		screen_startX=event.getSceneX();
		screen_startY=event.getSceneY();

		hBox.setBorder(new Border(new BorderStroke(Paint.valueOf("#FF7F24"), BorderStrokeStyle.SOLID, null, new BorderWidths(2))));
		shotAnchorPane.getChildren().addAll(hBox);
		//设置hbox的坐标点

		AnchorPane.setLeftAnchor(hBox, screen_startX);
		AnchorPane.setTopAnchor(hBox, screen_startY);

		label = new Label();
		//设置文字为白色
		label.setTextFill(Paint.valueOf("#FFFFFF"));
		//设置背景为黑色
		label.setStyle("-fx-background-color:#0F0F0F");

		shotAnchorPane.getChildren().add(label);
		AnchorPane.setLeftAnchor(label, screen_startX);
		AnchorPane.setTopAnchor(label, screen_startY);
	}

	//保存图片
	public void saveImage(){
		try {
			//显示图片到主界面
			Robot robot =new Robot();
			Rectangle rectangle = new Rectangle((int)screen_startX, (int)screen_startY, (int)width, (int)height);
			BufferedImage bi=robot.createScreenCapture(rectangle);
			WritableImage wi= SwingFXUtils.toFXImage(bi, null);
			imageView.setImage(wi);

			//将该图片保存在粘贴板
			javafx.scene.input.Clipboard cb= javafx.scene.input.Clipboard.getSystemClipboard();
			ClipboardContent  content = new ClipboardContent();
			content.putImage(wi);
			cb.setContent(content);

			//将该图片保存在磁盘上
			ImageIO.write(bi, "jpg", new File("F:/Log/1.jpg"));

			//将截图界面隐藏
			shotStage.close();

			//主界面展示
			primaryStage.setIconified(false);

		} catch (Exception e) {
			System.out.println("保存文件出错");
			e.printStackTrace();
		}


	}
}
